package com.cmcc.groupcontacts.firewall.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.cmcc.groupcontacts.firewall.dao.model.CallInfoVO;
import com.cmcc.groupcontacts.util.DateUtil;
import com.cmcc.groupcontacts.util.ValidationUtil;
import com.yinhai.activity.R;

/**
 * 
 * 从通话记录中选择号码Adapter
 * 
 * @author 
 * 
 */
public class SelectFromCallLogAdapter extends BaseAdapter<CallInfoVO> {
	/**
	 * 视图缓存
	 */
	public final class ViewCache {
		public TextView txtName;
		public TextView txtNumber;
		public TextView txtDate;
		public CheckBox chkSelect;
	}

	private ViewCache viewCache = null;
	private SparseBooleanArray selectedMap = null; // 存放多选项选中状态的Map

	public SparseBooleanArray getSelectedMap() {
		return selectedMap;
	}

	public SelectFromCallLogAdapter(Context context, ArrayList<CallInfoVO> callLogs) {
		super(R.layout.select_from_call_log_item, context, callLogs);
		selectedMap = new SparseBooleanArray(callLogs.size());
	}

	@Override
	protected void setViewCache(View convertView, int position) {
		viewCache = new ViewCache();
		viewCache.txtName = (TextView) convertView.findViewById(R.id.txt_name);
		viewCache.txtNumber = (TextView) convertView.findViewById(R.id.txt_number);
		viewCache.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
		viewCache.chkSelect = (CheckBox) convertView.findViewById(R.id.chk_select);
		convertView.setTag(viewCache);
	}

	@Override
	protected void getViewCache(View convertView, int position) {
		viewCache = (ViewCache) convertView.getTag();
	}

	@Override
	protected void setValueByViewCache(View convertView, int position) {
		final CallInfoVO callLog = getItem(position);
		// TODO: 参照集团通讯录通话记录界面修改
		if (!ValidationUtil.isNullOrEmpty(callLog.getName())) {
			viewCache.txtName.setText(callLog.getName());
		} else {
			viewCache.txtName.setText(callLog.getNumber());
		}
		viewCache.txtNumber.setText(callLog.getNumber());
		viewCache.txtDate.setText(DateUtil.dateToStr(new Date(callLog.getDate())));
		viewCache.chkSelect.setChecked(selectedMap.get(position));
	}
}
